package com.china08.yxyapi.service.mg.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrainQues;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnowTrainQuesMistake;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainQuesMistakeRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainQuesRepository;
import com.china08.yxyapi.service.mg.YxyHuixuePlanKnowTrainQuesMistakeService;

@Service
public class YxyHuixuePlanKnowTrainQuesMistakeServiceImpl  implements YxyHuixuePlanKnowTrainQuesMistakeService {
    
    @Autowired
    private YxyHuixuePlanKnowTrainQuesRepository yxyHuixuePlanKnowTrainQuesRepository;
    @Autowired
    private YxyHuixuePlanKnowTrainQuesMistakeRepository yxyHuixuePlanKnowTrainQuesMistakeRepository;
    @Override
    public void saveMistakes(String plandId) {
      List<YxyHuixuePlanKnowTrainQues>  yxyHuixuePlanKnowTrainQuesLst  =  yxyHuixuePlanKnowTrainQuesRepository.findByPlanIdAndPassed(plandId,0);
      YxyHuixuePlanKnowTrainQuesMistake yxyHuixuePlanKnowTrainQuesMistake=null;
      List<YxyHuixuePlanKnowTrainQues> yxyHuixuePlanKnowTrainQuesLst2 = null;
      for (YxyHuixuePlanKnowTrainQues yxyHuixuePlanKnowTrainQues : yxyHuixuePlanKnowTrainQuesLst) {
          yxyHuixuePlanKnowTrainQuesMistake = yxyHuixuePlanKnowTrainQuesMistakeRepository.findOne(yxyHuixuePlanKnowTrainQues.getPlanId()+"_"+yxyHuixuePlanKnowTrainQues.getKnowId()+"_"+yxyHuixuePlanKnowTrainQues.getQuesId());
          if (yxyHuixuePlanKnowTrainQuesMistake == null) {
              yxyHuixuePlanKnowTrainQuesMistake = new YxyHuixuePlanKnowTrainQuesMistake();
              yxyHuixuePlanKnowTrainQuesMistake.setId(yxyHuixuePlanKnowTrainQues.getPlanId()+"_"+yxyHuixuePlanKnowTrainQues.getKnowId()+"_"+yxyHuixuePlanKnowTrainQues.getQuesId());//(计划id+知识点id+题id, 中间使用"_")
              yxyHuixuePlanKnowTrainQuesMistake.setStudentId(yxyHuixuePlanKnowTrainQues.getStudentId());
              yxyHuixuePlanKnowTrainQuesMistake.setPlanId(yxyHuixuePlanKnowTrainQues.getPlanId());
              yxyHuixuePlanKnowTrainQuesMistake.setKnowId(yxyHuixuePlanKnowTrainQues.getKnowId());
              yxyHuixuePlanKnowTrainQuesMistake.setKnowName(yxyHuixuePlanKnowTrainQues.getKnowName());
              yxyHuixuePlanKnowTrainQuesMistake.setQuesId(yxyHuixuePlanKnowTrainQues.getQuesId());
              
              yxyHuixuePlanKnowTrainQuesLst2 = yxyHuixuePlanKnowTrainQuesRepository
                      .findDistinctYxyHuixuePlanKnowTrainQuesByStudentIdAndPlanIdAndKnowIdAndQuesIdAndPassed(
                              yxyHuixuePlanKnowTrainQues.getStudentId(), yxyHuixuePlanKnowTrainQues.getPlanId(), yxyHuixuePlanKnowTrainQues.getKnowId(), yxyHuixuePlanKnowTrainQues.getQuesId(), 0);
              yxyHuixuePlanKnowTrainQuesMistake.setMistakeCount(yxyHuixuePlanKnowTrainQuesLst2.size());//错误次数
          }
          yxyHuixuePlanKnowTrainQuesMistakeRepository.save(yxyHuixuePlanKnowTrainQuesMistake);
      }
    }
}
